package com.tanhua.dubbo.api;

import com.tanhua.model.LinHun.*;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class TestSoulImpl implements TestSoulApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    //查询问卷
    @Override
    public List<Soul> findAll() {
        Query query = new Query();
        List<Soul> list = mongoTemplate.find(query, Soul.class);
        return list;
    }

    @Override
    public List<Questions> findBySId(String sid) {
        Query query = Query.query(Criteria.where("sid").is(sid));
        return mongoTemplate.find(query, Questions.class);
    }

    @Override
    public List<Options> findByQidSid(String qid, String sid) {
        Query query = Query.query(Criteria.where("sid").is(sid).and("qid").is(qid));
        return mongoTemplate.find(query,Options.class);
    }

    @Override
    public String findByAnswers(String optionId, String questionId) {
        Criteria criteria = Criteria.where("qid").is(questionId).and("oid").is(optionId);
        Query query = new Query(criteria);
        List<Options> list = mongoTemplate.find(query, Options.class);
        String fen="";
        for (Options options : list) {
             fen = options.getFen();
        }
        return fen;
    }

    @Override
    public Xinge findByXingen(Integer zongfen) {
        return mongoTemplate.findOne(Query.query(Criteria.where("sore").lt(zongfen)),Xinge.class);
    }

    @Override
    public void save(Baogao baogao) {
        mongoTemplate.save(baogao);
    }

    @Override
    public Baogao findBy(String id) {
        return mongoTemplate.findOne(Query.query(Criteria.where("reportId").is(id)),Baogao.class);
    }

    @Override
    public List<Baogao> finds(Integer sore) {
        return mongoTemplate.find(Query.query(Criteria.where("sore").lt(sore)),Baogao.class);

    }


}
